package com.wy.hwreader.txtreader;

import com.wy.hwreader.data.BookMsgDB;
import com.wy.hwreader.readermain.Book;
import com.wy.hwreader.readermain.BookFileProcessor;
import com.wy.hwreader.readermain.CallBack;
import com.wy.hwreader.readermain.ReaderContex;
import com.wy.hwreader.readermain.ReaderException.Type;
import com.wy.hwreader.readermain.ReaderSettingInitProcessor;
import com.wy.hwreader.utils.FileCharsetDetector;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class TxtFileInitProcessor extends BookFileProcessor {

    @Override
    public void Process(CallBack<Type> callBack, ReaderContex readerContex) {

        Book book = readerContex.mBook;
        if (book == null) {
            throw new NullPointerException();
        }

        if (book.BookPath == null) {
            throw new NullPointerException();
        }

        if (!(new File(book.BookPath)).exists()) {
            callBack.onBack(Type.bookfilenofound);
            return;
        }

        initBookFileCode(book);
        initBookLength(book);
        initPreReadIndex(book, readerContex);
        ReaderSettingInitProcessor processor = new TxtReaderSettingInitProcessor();
        processor.Process(callBack, readerContex);
    }

    private void initPreReadIndex(Book book, ReaderContex readerContex) {

        BookMsgDB db = new BookMsgDB(readerContex.mContext);
        db.CreateTable();

        Book history = db.getBook(book.BOOKHashName);

        if (history != null) {
            book.PreReadParagraphIndex = history.PreReadParagraphIndex;
            book.PreReadCharIndex = history.PreReadCharIndex;
            book.PrePagestyle = history.PrePagestyle;
        } else {

            book.PreReadParagraphIndex = 0;
            book.PreReadCharIndex = 0;
        }
        db.close();
    }

    private void initBookLength(Book book) {
        try {
            book.BookLength = (new File(book.BookPath)).getTotalSpace();
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    private void initBookFileCode(Book book) {
        String code = "UTF-8";

        try {

            code = (new FileCharsetDetector()).guessFileEncoding(new File(book.BookPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        book.BookCode = code;
    }

}
